<template>
<div>
  <Modal v-model="mapMarkerForCommunityModal" title="社区中心点选取" fullscreen  @on-ok="mapMarkerForCommunityConfirm">
    <div id="mapMarkerForCommunity"></div>
  </Modal>
</div>
</template>

<script>
import { getCenter } from '../../../../libs/util'
import {
  // findGardenCommunityList,
  // findGardenStreetList,
  // findResidentialCommunityList,
  // findResidentialStreetList,
  newGetCompanyInfo
} from '../../../../api/z-new-grid'

export default {
  name: 'mapMarkerForCommunity',
  data () {
    return {
      map: null,
      mapMarkerForCommunityModal: false,
      choiceInfo: {},
      companyInfo: {},
      returnObject: {}
    }
  },
  methods: {
    init (e1, e2) {
      this.choiceInfo = e1
      this.companyInfo = e2
      this.newGetCompanyInfo()
    },
    newGetCompanyInfo () {
      let data = {
        id: this.companyInfo.id
      }
      newGetCompanyInfo(data).then(ret => {
        if (ret.data.errcode === 0) {
          this.companyInfo = ret.data.data
          this.dispostData()
        } else {
          this.$Notice.error({ title: '查询失败', desc: ret.data.errmsg })
        }
      })
    },
    dispostData () {
      if (this.companyInfo.csc_mapjson === undefined || this.companyInfo.csc_mapjson === '') {
        this.operationType = 'create'
        this.$Notice.error({ title: '未设置中心点！', desc: '将使用默认坐标展示！' })
        this.$set(this.companyInfo, 'csc_mapjson', '123.412581,41.805853')
      } else {
        this.operationType = 'update'
      }
      this.mapMarkerForCommunityModal = true
      let _this = this
      setTimeout(function () {
        _this.initMap(getCenter(_this.companyInfo, _this))
      }, 1000)
    },
    initMap (e) {
      this.map = new AMap.Map('mapMarkerForCommunity', {
        resizeEnable: true,
        center: new AMap.LngLat(e.lng, e.lat),
        zoom: 15
      })
      this.map.on('click', this.clickHandler)
      this.createMaker(e)
    },
    clickHandler (e) {
      this.lng = e.lnglat.lng
      this.lat = e.lnglat.lat
      let data = {
        lng: this.lng,
        lat: this.lat
      }
      this.clearMarker()
      this.createMaker(data)
    },
    clearMarker () {
      if (this.marker) {
        this.marker.setMap(null)
        this.marker = null
      }
    },
    createMaker (e) {
      this.marker = new AMap.Marker({
        position: new AMap.LngLat(e.lng, e.lat)
      })
      this.map.add(this.marker)
    },
    mapMarkerForCommunityConfirm () {
      let cscmapjson = this.marker.getPosition().lng + ',' + this.marker.getPosition().lat
      this.$set(this.returnObject, 'csc_mapjson', cscmapjson)
      this.$emit('returnMapMarkerInfoCommunity', this.returnObject)
    }
    // findResidentialStreetList () {
    //   findResidentialStreetList().then(ret => {
    //     if (ret.data.errcode === 0) {
    //       this.communityResidential = ret.data.data
    //       this.communityResidential.forEach(item => {
    //         if (item.csc_mapjson) {
    //           try {
    //             this.findCommunityResidentiallygon(JSON.parse(item.csc_mapjson))
    //           } catch (e) {
    //             console.log(e)
    //           }
    //         }
    //       })
    //     } else {
    //       this.$Notice.error({ title: '查询失败', desc: ret.data.errmsg })
    //     }
    //   })
    // },
    // findCommunityResidentialList () {
    //   findResidentialCommunityList().then(ret => {
    //     if (ret.data.errcode === 0) {
    //       this.communityResidential = ret.data.data
    //       this.communityResidential.forEach(item => {
    //         if (item.csc_mapjson) {
    //           try {
    //             this.findCommunityResidentiallygon(JSON.parse(item.csc_mapjson))
    //           } catch (e) {
    //             console.log(e)
    //           }
    //         }
    //       })
    //     } else {
    //       this.$Notice.error({ title: '查询失败', desc: ret.data.errmsg })
    //     }
    //   })
    // },
    // findGardenStreetList () {
    //   findGardenStreetList().then(ret => {
    //     if (ret.data.errcode === 0) {
    //       this.comunityGarden = ret.data.data
    //       this.comunityGarden.forEach(item => {
    //         if (item.csc_mapjson) {
    //           try {
    //             this.findCommunityGardenlygon(JSON.parse(item.csc_mapjson))
    //           } catch (e) {
    //             console.log(e)
    //           }
    //         }
    //       })
    //     } else {
    //       this.$Notice.error({ title: '查询失败', desc: ret.data.errmsg })
    //     }
    //   })
    // },
    // findCommunityGardenList () {
    //   findGardenCommunityList().then(ret => {
    //     if (ret.data.errcode === 0) {
    //       this.comunityGarden = ret.data.data
    //       this.comunityGarden.forEach(item => {
    //         if (item.csc_mapjson) {
    //           try {
    //             this.findCommunityGardenlygon(JSON.parse(item.csc_mapjson))
    //           } catch (e) {
    //             console.log(e)
    //           }
    //         }
    //       })
    //     } else {
    //       this.$Notice.error({ title: '查询失败', desc: ret.data.errmsg })
    //     }
    //   })
    // },
    // findCommunityResidentiallygon (e) {
    //   var path = []
    //   let data = e
    //   data.forEach(item => {
    //     path.push(new AMap.LngLat(item[0], item[1]))
    //   })
    //   var Polygon = new AMap.Polygon({
    //     path: path,
    //     strokeColor: '#E066FF',
    //     strokeWeight: 2,
    //     strokeStyle: 'solid',
    //     strokeOpacity: 1,
    //     fillOpacity: 0.1,
    //     fillColor: '#E066FF',
    //     zIndex: 40
    //   })
    //   Polygon.on('mouseover', function (e) {
    //     Polygon.setOptions({
    //       fillColor: '#EEDC82'
    //     })
    //   })
    //   Polygon.on('mouseout', function (e) {
    //     Polygon.setOptions({
    //       fillColor: '#E066FF'
    //     })
    //   })
    //   this.map.add(Polygon)
    // },
    // findCommunityGardenlygon (e) {
    //   var path = []
    //   let data = e
    //   data.forEach(item => {
    //     path.push(new AMap.LngLat(item[0], item[1]))
    //   })
    //   var Polygon = new AMap.Polygon({
    //     path: path,
    //     strokeColor: '#3CB371',
    //     strokeWeight: 2,
    //     strokeStyle: 'solid',
    //     strokeOpacity: 1,
    //     fillOpacity: 0.1,
    //     fillColor: '#3CB371',
    //     zIndex: 40
    //   })
    //   Polygon.on('mouseover', function (e) {
    //     Polygon.setOptions({
    //       fillColor: '#EEDC82'
    //     })
    //   })
    //   Polygon.on('mouseout', function (e) {
    //     Polygon.setOptions({
    //       fillColor: '#3CB371'
    //     })
    //   })
    //   this.map.add(Polygon)
    // }
  },
  beforeDestroy () {
    this.map && this.map.destroy()
  },
  watch: {
    mapMarkerForCommunityModal (val) {
      if (!val) {
        this.map && this.map.destroy()
      }
    }
  }
}
</script>

<style scoped>
#mapMarkerForCommunity {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}
</style>
